'use strict'

var Crewler = require('crawler');
var models = require('./models');
var https = require('https');
var fs = require('fs');


//声明变量，用来存储东西
var add = [];

var c = new Crewler({
    maxConnection: 1,
    rateLimit: 10,
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;

            var list = $('.dw_table .el').toArray();

            list.forEach(element => {
                //职业名称：Occupation
                //公司名称：CorporateName
                //地点：Site
                //工资：Salary
                //发布时间：PublishTime

                var Occupation = $(element).find(' .t1 span').text().trim().replace(/[ \r\n]/g, '');
                console.log(Occupation);//源代码中职业名称所在列的列名为t1

                var CorporateName = $(element).find(' .t2 a').attr('title');
                console.log(CorporateName);//源代码中公司名称所在列的列名为t2

                var Site = $(element).find(' .t3').text().trim().replace(/[ \r\n]/g, '');
                console.log(Site);//源代码中工作地点所在列的列名为t3

                var Salary = $(element).find(' .t4').text().trim().replace(/[ \r\n]/g, '');
                console.log(Salary);//源代码工资所在列的列名为t4

                var PublishTime = $(element).find(' .t5').text().trim().replace(/[ \r\n]/g, '');
                console.log(PublishTime);

                //前面是表的名字，后面才是你的命名
                models.create({ Occupation: Occupation, CorporateName: CorporateName, Site: Site, Salary: Salary, PublishTime: PublishTime })
            });


        };
        done();

    }
});

// setTimeout(() => {
//     for(let index =1;index<=15;index++){
//         c.queue('https://search.51job.com/list/010000%252C180200%252C020000%252C030200%252C040000,000000,7501,00,9,99,%2520,1,'+index+'.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=');
//     }
// }, 3);

for (let index = 1; index <= 15; index++) {
    c.queue('https://search.51job.com/list/010000%252C180200%252C020000%252C030200%252C040000,000000,7501,00,9,99,%2520,1,' + index + '.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=');
}

models.exports = c;
